﻿namespace WLFramework.Validation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class RequiredAttribute : ValidateBaseAttribute
    {
        public RequiredAttribute() :
            base("{0} 不能为空。", "请输入 {0}。")
        {
        }

        public RequiredAttribute(string errorMessage)
            : this(errorMessage, null)
        {
        }

        public RequiredAttribute(string errorMessage, string helpMessage)
            : base(errorMessage ?? "{0} 不能为空。", helpMessage ?? "请输入 {0}。")
        {
        }

        public override ValidateResult IsValid(ValidationContext context)
        {
            if (context.Value == null ||
                context.Value == DBNull.Value)
            {
                return new ValidateResult(
                    string.Format(this.ErrorMessage, context.DisplayName));
            }

            return ValidateResult.Success;
        }
    }
}
